package com.cfjjc.gz.funcpermission.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map; 

import org.apache.log4j.Logger;

import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.common.consts.TradeInfoConst;
import com.cfjjc.gz.common.dao.DefaultDAO;
import com.cfjjc.gz.common.exception.OTSException;
import com.cfjjc.gz.common.service.BaseService;
import com.cfjjc.gz.common.util.StringUtil;
import com.cfjjc.gz.common.util.RandomGUID; 
import com.cfjjc.gz.common.util.paged.PagedList;

import com.cfjjc.gz.funcpermission.vo.R_ROLEVO;
import com.cfjjc.gz.funcpermission.vo.R_USR_ROLEVO; 
import com.cfjjc.gz.usraccount.service.A_USRService;
import com.cfjjc.gz.usraccount.vo.A_USRVO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;   

/**
 * 
 * 说明：用户角色表 业务逻辑层对象
 * 
 * @author  liuxun
 * @since 2012-03-06
 * @version 1.0
 */ 
@Service
@Transactional(rollbackFor = Throwable.class)
public class R_USR_ROLEService extends BaseService{

    /**
     * logger对象
     */
    private static final Logger logger = Logger.getLogger(R_USR_ROLEService.class);

    @Autowired
    private DefaultDAO dao; 
    
    @Autowired
    private A_USRService a_USRService; 
    
/*    @Autowired
    private R_ROLEService r_ROLEService; */

    // 要访问的dao的命名空间
	private String namespace = "R_USR_ROLE";

    public R_USR_ROLEService() {
		super();
    }

    /**
     * 仅查询单条用户角色表的详细信息
     */
    public R_USR_ROLEVO queryR_USR_ROLE(R_USR_ROLEVO param) throws OTSException {
        return (R_USR_ROLEVO)dao.queryObject(param, this.namespace);
    }

    /**
     * 根据条件检索用户角色表，返回分页结果
     */
    public void queryR_USR_ROLEs(R_USR_ROLEVO param, PagedList records) throws OTSException {
        dao.queryPagedList(param, records, this.namespace);
    }

    /**
     * 根据条件检索用户角色表，返回所有结果
     */
    public List<R_USR_ROLEVO> queryR_USR_ROLEList(R_USR_ROLEVO param) throws OTSException {
        return dao.queryList(param, this.namespace);
    }

    /**
     * 根据用户信息，查询该用户已经选择和未选择的角色信息
     * @param param
     * @return
     * @throws OTSException
     */
    public List<Map> querySelectRoleList(R_USR_ROLEVO param) throws OTSException {
		return this.dao.queryList(param, this.namespace, "querySelectRole");
	}
    
    /**
     * 根据用户信息，查询该用户已经选择角色名称
     * @param param
     * @return
     * @throws OTSException
     */
    public List<Map> querySelectedRoleList(R_USR_ROLEVO param) throws OTSException {
		return this.dao.queryList(param, this.namespace, "querySelectedRole");
	}
    
    /**
     * 增加用户角色表
     */
    public void doInsertR_USR_ROLE(R_USR_ROLEVO param, String[] id) throws OTSException{

    	//删除所有的用户授权的角色
    	R_USR_ROLEVO query = new R_USR_ROLEVO();
    	query.setUsr_id(param.getUsr_id());
    	List <R_USR_ROLEVO> res = queryR_USR_ROLEList(query);
    	
    	A_USRVO queryUsr = new A_USRVO();
    	queryUsr.setUsr_id(param.getUsr_id());
    	A_USRVO usr = this.a_USRService.queryA_USR(queryUsr);
    	
    	for (Iterator<R_USR_ROLEVO> item = res.iterator(); item.hasNext();) {
    		R_USR_ROLEVO each = (R_USR_ROLEVO) item.next();
    		
	    	R_ROLEVO queryRole = new R_ROLEVO();
	    	queryRole.setRole_id(each.getRole_id());
	  //  	R_ROLEVO role = this.r_ROLEService.queryR_ROLE(queryRole);
	        
	//    insertTradeLog(TradeInfoConst.TRADE_DEL_USR_ROLE, usr.getUsr_name()+ "/" + role.getRole_name(), param);
    		
    		dao.delete(each, this.namespace);
    	}
    	
		for (int i = 0; i < id.length; i++) {
	        param.setKey(RandomGUID.getRandomGuid());
	        param.setRole_id(id[i]);
	        
	    	//录入前的校验
	    	_checkFields(param,SystemConst.OPERATION_TYPE_NEW);
	        dao.insert(param, this.namespace);
	        if (logger.isDebugEnabled()) {
	            logger.debug("增加一条用户角色表记录:" + param);
	        }
	        
	    	R_ROLEVO queryRole = new R_ROLEVO();
	    	queryRole.setRole_id(id[i]);
	  //  	R_ROLEVO role = this.r_ROLEService.queryR_ROLE(queryRole);
	        
	 //       insertTradeLog(TradeInfoConst.TRADE_ADD_USR_ROLE, usr.getUsr_name()+ "/" + role.getRole_name(), param);
		}
    }

    /**
     * 修改用户角色表
     */
    public void doUpdateR_USR_ROLE(R_USR_ROLEVO param) throws OTSException {
    	//修改前的校验
    	_checkFields(param,SystemConst.OPERATION_TYPE_MODIFY);
        dao.update(param, this.namespace);
        if (logger.isDebugEnabled()) {
            logger.debug("修改用户角色表:" + param);
        }
    }

    /**
     * 删除用户角色表
     */
    public void doDeleteR_USR_ROLE(R_USR_ROLEVO param) throws OTSException {
        //删除前的校验
    	_checkFields(param,SystemConst.OPERATION_TYPE_DELETE);
        dao.delete(param, this.namespace);
        if (logger.isDebugEnabled()) {
            logger.debug("删除用户角色表:" + param);
        }
    }  

	/** 以下为私有方法.私有方法应以下划线"_"开头 */
	/**
	 * 对当前的操作对象进行字段级的校验<br>
	 * @param param
	 * 			待操作的vo对象
	 * @param opeType
	 *			操作类型,见SystemConst中定义的常数
	 *
	 * @see com.mofit.fs.yhbs.common.SystemConst
	 */
    private void _checkFields(R_USR_ROLEVO param,int opeType) throws OTSException {
	 	if(logger.isDebugEnabled()){
	 		logger.debug("对待保存的对象进行字段校验,操作类型opeType="+opeType);
	 	}
	 	/*
	 	 * 除操作类型为insert外,主键字段不能为空
	 	 */
	 	if(opeType!=SystemConst.OPERATION_TYPE_NEW){

	   		 if(param.getAuto_id() == null || "".equals(String.valueOf(param.getAuto_id()))){
	        	throw new OTSException("自动生成ID不能为空！");
	    	 }

	 	}
	 	/*
	 	 *对于操作类型为insert,非主键字段中不允许null的字段不能为空
	 	 */
		if(opeType==SystemConst.OPERATION_TYPE_NEW || opeType== SystemConst.OPERATION_TYPE_MODIFY){

	   		 if(param.getUsr_id() == null || "".equals(String.valueOf(param.getUsr_id()))){
	        	throw new OTSException("用户ID不能为空！");
	    	 }

	   		 if(param.getRole_id() == null || "".equals(String.valueOf(param.getRole_id()))){
	        	throw new OTSException("角色ID不能为空！");
	    	 }

		}

	 	/*
	 	 *对于操作类型为insert或update,字段长度不能超过表中字段长度
	 	 */
		if(opeType==SystemConst.OPERATION_TYPE_NEW||opeType==SystemConst.OPERATION_TYPE_MODIFY){

       		if(StringUtil.getLength(param.getUsr_id())>50){
    			throw new OTSException("用户ID内容长度不能大于50位,请检查！");    
  			} 

       		if(StringUtil.getLength(param.getRole_id())>50){
    			throw new OTSException("角色ID内容长度不能大于50位,请检查！");    
  			} 

		}

		if(logger.isDebugEnabled()){
	 		logger.debug("字段校验通过");
	 	}	 	
	}
}